package struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import pessoal.cliente.ClienteFiel;
import pessoal.cliente.exception.ClienteInvalidoException;
import pessoal.funcionario.Funcionario;
import pessoal.funcionario.exception.FuncionarioInvalidoException;

import suite.SuiteAbstrata;
import suite.exception.SuiteInvalidaException;
import suite.exception.SuiteUtilizadaException;
import util.ErroAcessoRepositorioException;

import fachada.Fachada;
import funcional.reserva.ReservaSuite;
import funcional.reserva.exception.ReservaInvalidaException;
import funcional.reserva.exception.SuiteJaReservadaException;

public class ReservaDispatchAction extends DispatchAction {
	public ActionForward listar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("listar");
	}
	
	public ActionForward formReserva(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("formReserva");
	} 
	
	public ActionForward detalhar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			ReservaSuite reserva = Fachada.obterInstancia().consultarReserva(Integer.parseInt(request.getParameter("suite")));
			request.setAttribute("tipoSuite", reserva.getSuite().getClass().getName().substring(11));
			request.setAttribute("cpfFuncionario", reserva.getFuncionario().getCpf());
			request.setAttribute("nomeFuncionario", reserva.getFuncionario().getNome());
			request.setAttribute("cpfCliente", reserva.getCliente().getCpf());
			request.setAttribute("nomeCliente", reserva.getCliente().getNome());
			request.setAttribute("descontoCliente", reserva.getCliente().getDesconto());
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ReservaInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("formDetalhar");
	}
	
	/*public ActionForward formAlterar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			ReservaSuite reserva = Fachada.obterInstancia().consultarReserva(Integer.parseInt(request.getParameter("suite")));
			request.setAttribute("numSuite", reserva.getSuite().getNumeroSuite());
			request.setAttribute("tipoSuite", reserva.getSuite().getClass().getName().substring(11));
			request.setAttribute("nomeFuncionario", reserva.getFuncionario().getNome());
			request.setAttribute("nomeCliente", reserva.getCliente().getNome());
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ReservaInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("formAlterar");
	}*/
	
	/*public ActionForward alterar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			Funcionario funcionario = Fachada.obterInstancia().consultarFuncionario(request.getParameter("funcionario"));
			ClienteFiel cliente = Fachada.obterInstancia().consultarClienteFiel(request.getParameter("cliente"));
			SuiteAbstrata suite = Fachada.obterInstancia().consultarSuite(Integer.parseInt(request.getParameter("suite")));
			Fachada.obterInstancia().atualizarReserva(new ReservaSuite(suite,funcionario,cliente));
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (FuncionarioInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ClienteInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (SuiteInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ReservaInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		} 
		return mapping.findForward("listar");
	}*/
	
	public ActionForward remover(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			Fachada.obterInstancia().excluirReserva(Integer.parseInt(request.getParameter("suite")));
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ReservaInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("listar");
	}
	
	public ActionForward inserir(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		Fachada fachada = Fachada.obterInstancia();
		try {
			SuiteAbstrata suite = fachada.consultarSuite(Integer.parseInt(request.getParameter("suite")));
			Funcionario funcionario = fachada.consultarFuncionario(request.getParameter("funcionario"));
			ClienteFiel cliente = fachada.consultarClienteFiel(request.getParameter("cliente"));
			fachada.inserirReserva(new ReservaSuite(suite,funcionario,cliente));
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (SuiteInvalidaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (FuncionarioInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ClienteInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (SuiteJaReservadaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (SuiteUtilizadaException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		} 
		return mapping.findForward("listar");
	}
}
